package com.intel.two.controller.building;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.intel.two.bo.building.RegisterBo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.listener.building.WebRegisterBoListener;
import com.intel.two.pojo.building.Register;
import com.intel.two.service.building.RegisterService;
import com.intel.two.vo.building.RegisterBarVo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/register")
public class RegisterController {
    @Autowired
    private RegisterService registerService;

    @RequestMapping("/registerList")
    public LayuiPageVo<Register> registerPageData(@RequestParam Map<String,Object> params){
        System.out.println("接收的参数"+params);
        return registerService.registerPageData(params);
    }

    @RequestMapping("addRegister")
    public R addRegister(@RequestBody Register register){

        System.out.println("添加用户"+register);
        int result = registerService.addRegister(register);
        if (result == 1) {
            return R.ok();
        }else{
            return R.error();
        }

    }

    @RequestMapping("delete")
    public R deleteRegister (Integer id){
        System.out.println("要删除的访客id："+id);
        int result = registerService.deleteRegister(id);
        if (result == 1) {
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping("/deleteRegisters")
    public R deleteRegisters(@RequestParam Map<String, Object> map) {
        System.out.println(map);
        int result = registerService.deleteRegisters(map);
        return result >0 ? R.ok() : R.error();
    }

    @RequestMapping("updateRegister")
    public R updateRegister(Register register){
        System.out.println("更新的用户"+register);
        int result = registerService.updateRegister(register);
        if (result == 1) {
            return R.ok();
        }else{
            return R.error();
        }
    }

    @RequestMapping("echartBar")
    public R echartBar(){
        List<RegisterBarVo> barData= registerService.BarData();
        System.out.println(barData);

        return R.ok().data("barData",barData);
    }

    @RequestMapping("/export")
    public void export(Register register, HttpServletResponse response) throws IOException {
        System.out.println("输出excel文件");
        //下载 excel文件 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止中文亂碼
        String filename = URLEncoder.encode("访客信息导出","utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        // 获取输出流
        ServletOutputStream outputStream = response.getOutputStream();

        // easyexcel 写数据到 输出流
        ExcelWriterBuilder write = EasyExcel.write(outputStream, Register.class);
        ExcelWriterSheetBuilder sheet = write.sheet();

        //列表数据
        List<RegisterBo> residentExcelData =registerService.RegisterExcelData(register);
        sheet.doWrite(residentExcelData);
    }

    @Autowired
    private WebRegisterBoListener webRegisterBoListener;
    @RequestMapping("/import")
    public R importRegister(MultipartFile file) throws IOException {
        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), RegisterBo.class, webRegisterBoListener);

        ExcelReaderSheetBuilder sheet = read.sheet();

        sheet.doRead();

        return R.ok();
    }

    @RequestMapping("/templateDownload")
    public void templateDownload (HttpServletRequest request, HttpServletResponse response) throws IOException {
        //下载 excel文件 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        String realPath = request.getServletContext().getRealPath("/excel/访客列表模板.xlsx");
        //得到文件对象
        File file = new File(realPath);
        //文件对象转为字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //防止中文亂碼
        String filename = URLEncoder.encode("访客列表模板","utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(bytes);

        outputStream.close();

    }
}
